import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children:[
      {
        path: '/',
        name: 'user',
        component: () => import('../views/user.vue')
      },
      {
        path: 'jurisdiction',
        name: 'jurisdiction',
        component: () => import('../views/jurisdiction.vue')
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('../views/role.vue')
      },
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login.vue')
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 登录路由守卫
// to即将进入的路由对象。from即将离开的路由对象。next执行下一个函数
router.beforeEach((to,from,next)=>{
  var userLogin=localStorage.getItem('token') //当前状态未登录
  if (to.path!='/login'){  //如果当前路由不是login，那么先执行下面的命令
    if (userLogin){   //如果是已经登录，next（）放行
      next()
    }else { //否则未登录，就跳到login路由,进行注册
      next('/login')
    }
  }else { //如果上面的都没执行，就直接放行
    next()
  }
})


export default router
